#line 2 "F:\Emergent\Gamebryo-2.6-Source\Samples\Demos\TerrainDemo\Data\Shaders\Generated\NiWaterMaterial\00000001-00000001-00000000-00000000-V.hlsl"
/*
Shader description:
TANGENTS = 1
USE_PLANAR_REFLECTION = 0
USE_CUBE_MAP_REFLECTION = 0
NUM_TEXCOORDS = 1
*/

//---------------------------------------------------------------------------
// Types:
#if defined(DIRECT3D)
#define BONEMATRIX_TYPE float4x3
#else
#define BONEMATRIX_TYPE float3x4
#endif
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Constant variables:
//---------------------------------------------------------------------------

float4x4 g_World;
float4 g_EyePos;
float4x4 g_ViewProj;
float4x4 g_TexTransformNormal;
float2 NormalMapModifier;
//---------------------------------------------------------------------------
// Functions:
//---------------------------------------------------------------------------

/*

      This fragment is responsible for applying the world transform to the
      incoming local vertex position.
    
*/

void TransformPositionToWorld(float3 Position,
    float4x4 World,
    out float4 WorldPos)
{

      WorldPos = mul(float4(Position.xyz, 1.0), World);
    
}
//---------------------------------------------------------------------------
/*

    This fragment is responsible for applying the world transform to the
    normal, binormal, and tangent.
    
*/

void TransformNT(float3 Normal,
    float3 Tangent,
    float4x4 World,
    out float3 WorldNrm,
    out float3 WorldTangent)
{

    // Transform the normal into world space for lighting
    WorldNrm      = mul( Normal, (float3x3)World );
    WorldTangent  = mul( Tangent, (float3x3)World );
    
    // Should not need to normalize here since we will normalize in the pixel 
    // shader due to linear interpolation across triangle not preserving
    // normality.
    
}
//---------------------------------------------------------------------------
/*

      This fragment is responsible for applying the view projection transform
      to the input world position.
    
*/

void ProjectPositionWorldToProj(float4 WorldPosition,
    float4x4 ViewProjMatrix,
    out float4 WorldProj)
{

      WorldProj = mul(WorldPosition, ViewProjMatrix);
    
}
//---------------------------------------------------------------------------
/*

      This fragment is responsible for calculating the camera view vector.
    
*/

void CalculateViewVectorDistance(float4 WorldPos,
    float3 CameraPos,
    out float3 WorldViewVector,
    out float ViewDistance)
{

      WorldViewVector = CameraPos - WorldPos;
      ViewDistance = length(WorldViewVector);
    
}
//---------------------------------------------------------------------------
/*

      This fragment works out the different texture values from the 
      texture stream
    
*/

void SplitTextureCoordinates(float2 TexCoord,
    float4x4 TexTransform,
    float2 NormalModifier,
    out float2 BaseUV,
    out float2 NormalUV)
{

        BaseUV = TexCoord.xy;
        NormalUV.xy = BaseUV.xy * NormalModifier.xy;
        NormalUV = mul(float4(NormalUV.x, NormalUV.y, 0.0, 1.0), TexTransform);
    
}
//---------------------------------------------------------------------------
/*

      Takes the cross product of a vertex normal and a tangent to create a binormal.      
      Assumes the vertex normal and tangent are normalized.
    
*/

void CalculateBinormal(float3 WorldNormal,
    float3 WorldTangent,
    out float3 WorldBinormal)
{

      WorldBinormal = cross(WorldTangent, WorldNormal);
    
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Input:
//---------------------------------------------------------------------------

struct Input
{
    float3 Position : POSITION0;
    float3 Normal : NORMAL0;
    float2 TexCoord : TEXCOORD0;
    float3 Tangent : TANGENT0;

};

//---------------------------------------------------------------------------
// Output:
//---------------------------------------------------------------------------

struct Output
{
    float4 WorldProj : POSITION0;
    float4 WorldPosition : TEXCOORD0;
    float3 WorldView : TEXCOORD1;
    float ViewDistance : TEXCOORD2;
    float3 WorldNormal : TEXCOORD3;
    float3 WorldTangent : TEXCOORD4;
    float3 WorldBinormal : TEXCOORD5;
    float2 BaseUV : TEXCOORD6;
    float2 NormalUV : TEXCOORD7;

};

//---------------------------------------------------------------------------
// Main():
//---------------------------------------------------------------------------

Output Main(Input In)
{
    Output Out;
	// Function call #0
    TransformPositionToWorld(In.Position, g_World, Out.WorldPosition);

	// Function call #1
    TransformNT(In.Normal, In.Tangent, g_World, Out.WorldNormal, 
        Out.WorldTangent);

	// Function call #2
    ProjectPositionWorldToProj(Out.WorldPosition, g_ViewProj, Out.WorldProj);

	// Function call #3
    CalculateViewVectorDistance(Out.WorldPosition, g_EyePos, Out.WorldView, 
        Out.ViewDistance);

	// Function call #4
    SplitTextureCoordinates(In.TexCoord, g_TexTransformNormal, 
        NormalMapModifier, Out.BaseUV, Out.NormalUV);

	// Function call #5
    CalculateBinormal(Out.WorldNormal, Out.WorldTangent, Out.WorldBinormal);

    return Out;
}

